package com.yaqian.wolf.controller;


import com.yaqian.wolf.comment.Result;
import com.yaqian.wolf.entity.Floorlist;
import com.yaqian.wolf.entity.Roomdetail;
import com.yaqian.wolf.repository.RoomdetailRepisitory;
import com.yaqian.wolf.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rent")
public class RoomHandler {
    @Autowired
    private com.yaqian.wolf.repository.FloorlistRepisitory floorlistRepisitory;
    @Autowired
    private RoomdetailRepisitory roomdetailRepisitory;
    @Autowired
    HttpServletRequest request;

    //    @GetMapping("/room")
//    public Page<Roomdetail> ok (){
//        List ji =new ArrayList<Roomdetail>();
//        System.out.println(roomdetailRepisitory.findAllByFloor("1"));
//        Pageable pageable =PageRequest.of(1,2);
////        System.out.println(roomdetailRepisitory.findAllByFloor(pageable,"1"));
//       return roomdetailRepisitory.findAllByFloor(pageable,"1");
//    }


    @GetMapping("/roompage/{page}/{size}/{key}")
    public Page<Roomdetail> pages(@PathVariable("page")Integer page, @PathVariable("size") Integer size,
    @PathVariable("key") String key){
        Pageable pageable = PageRequest.of(page-1,size);
        return roomdetailRepisitory.findAllByFloor(pageable,key);
    }
    @PostMapping("/addRoom")
    public Result addUser(@RequestBody Roomdetail roomdetail){
        Result res =new Result();
        try {
            Roomdetail ok = roomdetail;
            System.out.println(roomdetailRepisitory.save(ok));
            res.success();
            return res;
        }catch (Exception e)
        {
            res.addError();
            return res;
        }
    }
//
    @GetMapping("/findRoom/{key}")
    public Result Find(@PathVariable String key){
        Result res =new Result();
        try {
            System.out.println(key);
            Roomdetail roomdetail = roomdetailRepisitory.findById(key).get();
            System.out.println("asdas"+roomdetail);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("roomdetail", roomdetail);
            res.setData(map);
            res.success();
            return res;
        }catch (Exception e)
        {
            res.addError();
            return res;
        }
    }
////
//
//    @GetMapping("/deleteRoom/{key}")
//    public Result delete(@PathVariable String key){
//        Result res =new Result();
//        try {
//            System.out.println(key);
//            roomdetailRepisitory.deleteById(key);
//            res.success();
//            return res;
//        }catch (Exception e)
//        {
//            res.addError("没有这个用户名或者其他原因");
//            return res;
//        }
//    }
}
